package handler

import (
	"925/api/basic/config"
	"925/api/pkg"
	req2 "925/api/req"
	__ "925/video-srv/basic/proto"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)

func VideoAdd(ctx *gin.Context) {
	var req req2.VideoAddReq
	ctx.ShouldBind(&req)
	value := ctx.GetHeader("title")
	r, err := config.Video.VideoAdd(ctx, &__.VideoAddReq{
		Title:  value,
		CovUrl: req.CovUrl,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "添加成功",
		"data":    &__.VideoAddResp{VideoId: r.VideoId},
	})
}
func Login(ctx *gin.Context) {
	var req req2.Login
	ctx.ShouldBind(&req)
	r, err := config.Video.Login(ctx, &__.LoginReq{
		Name: req.Name,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	handler, _ := pkg.TokenHandler(int(r.UserId), int(r.IsVip))

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "登录成功",
		"data":    handler,
	})
}
func Vip(ctx *gin.Context) {
	var req req2.VipReq
	ctx.ShouldBind(&req)
	userId := ctx.GetHeader("userId")
	userIds, _ := strconv.Atoi(userId)
	isVip := ctx.GetHeader("isVip")
	isvips, _ := strconv.Atoi(isVip)
	r, err := config.Video.Vip(ctx, &__.VipReq{
		UserId:  int64(userIds),
		IsVip:   int64(isvips),
		VideoId: int64(req.VideoId),
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "查看成功成功",
		"data": &__.VipResp{
			VideoId: r.VideoId,
			Message: r.Message,
		},
	})
}
